<?
class Menu_model extends CI_Model
{	
	function __construct()
	{
		parent::__construct();
		
	}
	private function _required($required, $data)
	{
		foreach($required as $field) if(!isset($data[$field])) return false;
		return true;
	}
	
	private function _default($defaults, $options)
	{
		return array_merge($defaults, $options);
	}
	function insert($options = array())
	{
		//if(!$this->_required(array(.$qualification_fields.), $options)) 
		//		return false;
			if($options['data'])
			$options['data'] = json_encode($options['data']);

		//$options = $this->_default(array('u_created_on' => sql_current_datetime()), $options);
		$qualificationArray = array('id', 'parent', 'type', 'data', 'area', 'order', 'default');
		foreach($qualificationArray as $qualifier)
			if(isset($options[$qualifier])) 
				$this->db->set($qualifier, $options[$qualifier]);
	 
		$this->db->insert('menus');
	
		return $this->db->insert_id();
	}
	
	function update($options = array())
	{
		if(!$this->_required(array('id'), $options)) 
			return false;

		$qualificationArray = array('id', 'parent', 'type', 'data', 'area', 'order', 'default');
		foreach($qualificationArray as $qualifier)
			if(isset($options[$qualifier])) 
				$this->db->set($qualifier, $options[$qualifier]);
	
		$this->db->where('id', $options [ 'id' ]);	
		$this->db->update('menus');	
		return $this->db->affected_rows();
	}
	
	function get($options = array())
	{
		$options = $this->_default(array('sort_direction' => 'asc'), $options);
	
		$qualificationArray = array('id', 'parent', 'type', 'data', 'area', 'order', 'default');
		foreach($qualificationArray as $qualifier)
			if(isset($options[$qualifier])) 
				$this->db->where($qualifier, $options[$qualifier]);
	
		if(isset($options ['limit']) && isset($options ['offset'])) 
			$this->db->limit($options ['limit'], $options ['offset']);
		else if(isset($options ['limit'])) 
			$this->db->limit($options ['limit']);
	
		if(isset($options ['sort_by'])) 
		$this->db->order_by($options ['sort_by'], $options ['sort_direction']);
		
		$query = $this->db->get('menus');
		
		if($query->num_rows() == 0) 
			return false;
	
		if(isset($options ['id']))
		{
			$result = $query->row_array(0);
			$result['data'] = json_decode($result['data'], TRUE);
		}
		else
		{
			$result = $query->result_array();
			for($i = 0; $i < count($result); $i++)
			{
				$result[$i]['data'] = json_decode($result[$i]['data'], TRUE);
			}
		}
		
		return $result;
	}
	
	function delete($options = array())
	{
		if(!$this->_required(array('id'), $options)) 
			return false;
	
        $item = $this->get(array('id' => $options['id']));
            
        // Give the children a new parent
        $childs = $this->get(array('parent' => $options['id']));
		if($childs)
	        foreach($childs as $child)
	        {
	            $this->db
	                ->where('parent', $item['id'])
	                ->delete('menus');
	        }		
	
		$this->db->where('id', $options ['id']);
		$this->db->delete('menus');
		return $this->db->affected_rows();
	}
	
	function add($name, $area)
	{		
		foreach($this->config->item('site_supported_languages') as $language)
		{
			$data['names'][$language] = $name;
		}		
		
		return $this->insert(array('type' => 'blank',
						  		'area' => $area,
								'data' => $data));
	}
	
	
    
    function set_default($id)
    {
        // un-default
        $this->db->where('default', 1)->set('default', 0)->update('menus');
        
        // Defaultize
        $this->db->where('id', $id)->set('default', 1)->update('menus');
        
    }
}